package Webpage;
import AutoTest.AutoFun;
import Commons.CommonsFun;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import java.time.Duration;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-09-09
 * Time: 22:25
 */
//商城首页各功能测试
public class HomePage {
    WebDriver driver=null;
    String url="http://8.155.1.153/";
    public void test(){
        driver=AutoFun.getDriver();
        test1();//网站LOGO交互
        System.out.println("logo测试完成");
        test2();//轮换图片交互
        System.out.println("轮换图片测试完成");
        test3();//顶部导航栏交互
        System.out.println("顶部导航栏测试完成");
        test4();//商品搜索框交互---
        System.out.println("商品搜索框测试完成");
        test5();//商品分类栏交互---
        System.out.println("商品分类栏测试完成");
        test6();//个人信息栏交互
        System.out.println("个人信息栏测试完成");
        test7();//展示商品交互
        System.out.println("展示商品交互完成");
        System.out.println("商城主页测试完成");
    }

    private void test1(){
        //与LOGO交互，并进行截图查看交互情况
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div[1]/a/img")).click();
        try {
            Thread.sleep(2000);
        }
        catch (InterruptedException e){
            e.printStackTrace();
        }
        CommonsFun.altA(driver);
    };
    private void test2(){
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[5]/div/ol/li[1]/a")).click();
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[5]/div/div/ul/li[2]/a/img"));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[5]/div/ol/li[2]/a")).click();
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[5]/div/div/ul/li[3]/a/img"));
    };
    private void test3(){
        //个人中心
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div/ul[2]/div[1]/div/a")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[4]/div[3]/div/div[1]/div[1]/div[1]/div/span"));
        //我的商城
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div/ul[2]/div[3]/div/a")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div/ul[2]/div[3]/div/ul")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div[3]/div/div[3]/form/div[2]/button"));
        //我的收藏
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[1]/div/ul[2]/div[4]/div/a")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[1]/div/ul[2]/div[4]/div/ul")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div[3]/div/div[1]/h1"));
        //更多入口
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[1]/div/ul[2]/div[5]/div/a")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[1]/div/ul[2]/div[5]/div/ul/li[1]")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[5]/div/div[2]/div/div/ul/li[1]"));
        //购物车
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div/ul[2]/div[6]/div/a")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[5]/div[2]/div/div[2]/form/button"));
        //消息
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div/ul[2]/div[7]/div/a")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div[3]/div/div[1]/h1"));
    };
    private void test4(){
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div/div[1]/a/img")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        //不输入内容进行搜索
        driver.findElement(By.xpath("//*[@id=\"ai-topsearch\"]")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("//*[@id=\"goods-category\"]/div/a/div/span"));
        //输入过长内容进行搜索
        driver.findElement(By.xpath("//*[@id=\"search-input\"]")).sendKeys("1111111111111111111111111111111111111111");
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("//*[@id=\"goods-category\"]/div/a/div/span"));
        //输入特殊字符进行搜索
        driver.findElement(By.xpath("//*[@id=\"search-input\"]")).clear();
        driver.findElement(By.xpath("//*[@id=\"search-input\"]")).sendKeys("& %*");
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("//*[@id=\"goods-category\"]/div/a/div/span"));
        //正常搜索，对搜索结果进行筛选和排序
        driver.findElement(By.xpath("//*[@id=\"search-input\"]")).clear();
        driver.findElement(By.xpath("//*[@id=\"search-input\"]")).sendKeys("电脑");
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("//*[@id=\"search-map\"]/div/div/ul/li[3]/div[2]/ul/li[1]/a")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[5]/div/div[3]/div/ul/li[5]")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("//*[@id=\"goods-category\"]/div/a/div/span"));
    };
    private void test5(){
        //通关商品分类栏选择商品，对商品进行筛选和排序
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[2]/div/div[1]/a/img")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("//*[@id=\"goods-category\"]/div/a/div")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[5]/div/div[2]/div/div/ul/li[2]")).click();
        driver.findElement(By.xpath("//*[@id=\"goods-category\"]/div/a/div"));
    };
    private void test6(){
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[3]/div/div[1]/a/img")).click();
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[6]/div/div[1]/div[2]/a[1]"));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[6]/div/div[1]/div[2]/a[2]"));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[6]/div/div[1]/div[2]/a[3]"));
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[6]/div/div[1]/div[2]/a[4]"));
    };
    private void test7(){
        driver.findElement(By.xpath("//*[@id=\"floor1\"]/div[2]/div[2]/div/ul/li[1]/div")).click();
        String handle1= driver.getWindowHandle();
        Set<String> handles= driver.getWindowHandles();
        for (String handle:handles) {
            if(handle!=handle1){
                driver.switchTo().window(handle);
            }
        }
        driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[5]/div[2]/div[2]/div/div[3]/div[2]/button[1]"));
        driver.close();
        driver.switchTo().window(handle1);
    };
}
